package com.zhang.sdk.utils.app;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 共享变量工具类
 *
 * 功能：包括共享变量存取等方法
 * @author zhang.x
 */
public class SharedPreferenceUtil {

    public static SharedPreferences getSharedPreferences(Context context, String name) {
        return context.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    public static String getSharedPreferencesValue(Context context, String name, String key, String defValue){
        SharedPreferences sharedPreferences = getSharedPreferences(context, name);
        return sharedPreferences.getString(key, defValue);
    }

    public static void setSharedPreferences(Context context, String name, String[] keys, String[] values) {
        SharedPreferences sharedPreferences = getSharedPreferences(context, name);
//        SharedPreferences.Editor edit = sharedPreferences.edit();
        SharedPreferences.Editor edit = sharedPreferences.edit();
        for(int i = 0 ;i < keys.length ;i++){
            edit.putString(keys[i],values[i]);
        }
        edit.apply();
    }

    public static void setSharedPreferenceValue(Context context, String name, String key, String value) {
        SharedPreferences sharedPreferences = getSharedPreferences(context, name);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(key, value);
        edit.apply();
    }
}
